/*
 * TCPServerPacket.h
 *
 *  Created on: Sep 16, 2017
 *      Author: zjf
 */

#ifndef SRC_TCPSERVERPACKET_H_
#define SRC_TCPSERVERPACKET_H_

#include <string.h>
#include <Poco/AutoPtr.h>
#include "Packet.h"

using namespace Poco;
using namespace utils;

namespace network
{

struct TCPServerPacketHeader
{
	bool blZipFlag : 1;
	Int32 nChannelID : 7;
	Int32 nDataLen : 24;
};

class TCPServerPacket : public Packet
{
public:
	typedef AutoPtr<TCPServerPacket> Ptr;

	TCPServerPacket();
	virtual ~TCPServerPacket();

//	TCPServerPacket(TCPServerPacket* packet)
//	{
//		_header = packet->_header;
//		allocData();
//		memcpy(_pszData, packet->_pszData, (UInt32)packet->getDataSize());
//	}
//
	TCPServerPacket(TCPServerPacket& packet);

	TCPServerPacket(TCPServerPacket&& packet);

//	TCPServerPacket& operator = (TCPServerPacket&& packet)
//	{
//		_header = packet._header;
//		_pszData = packet._pszData;
//		packet._pszData = nullptr;
//
//		return *this;
//	}
//
	TCPServerPacket& operator = (TCPServerPacket& packet);

	Int32 getChannelID();
	bool isZiped();
	void moveData(char*& pBuf);

	virtual char* getHeader();
	virtual Int32 getHeaderSize();
	virtual bool validateHeader(std::string& strErrMsg);
	virtual void allocData();
	virtual char *getData();
	virtual Int32 getDataSize();
	virtual bool validateData(std::string& strErrMsg);
private:
	TCPServerPacketHeader _header;
	char *_pszData = nullptr;
};

class TCPServerDispatcherPacket : public RefCountedObject
{
public:
	typedef AutoPtr<TCPServerDispatcherPacket> Ptr;

	TCPServerDispatcherPacket(TCPServerPacket::Ptr& packet);

	virtual ~TCPServerDispatcherPacket();

	TCPServerDispatcherPacket(TCPServerDispatcherPacket& packet);

	TCPServerDispatcherPacket(TCPServerDispatcherPacket* packet);

	TCPServerDispatcherPacket(TCPServerDispatcherPacket&& packet);

	TCPServerDispatcherPacket& operator = (TCPServerDispatcherPacket& packet);

	TCPServerDispatcherPacket& operator = (TCPServerDispatcherPacket* packet);

	TCPServerDispatcherPacket& operator = (TCPServerDispatcherPacket&& packet);

private:
	Int32 _nDataLen = 0;
	char* _pDataBuf = nullptr;
};

} /* namespace network */

#endif /* SRC_TCPSERVERPACKET_H_ */
